package binaryTree;

/*
 *@description 求根节点到叶节点数字之和
 *@author hkfan
 *@version 1.0
 *@create 2025/6/16 15:36
 */
public class LeetCode_129 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public int sumNumbers(TreeNode root) {
        return solution(root,0);
    }

    private int solution(TreeNode root, int sum) {
        if (root!=null && root.left==null && root.right==null){
            return sum*10+root.val;
        }
        if (root==null){
            return 0;
        }
        return solution(root.left,sum*10+root.val)+solution(root.right,sum*10+root.val);
    }
}
